﻿@using Seal.Model;
@{
    ReportTask task = Model;

    task.TemplateDescription = "Load a table from another Data Source into the current database.";

    //Parameters for this task
    task.Parameters.Add(new Parameter() { Name = "source_name", Value = "", DisplayName = "Data Source Name", Description = "The name of the DataSource used to perform the SQL Select Statement." });
    task.Parameters.Add(new Parameter() { Name = "sql", Value = "", DisplayName = "SQL Select Statement", Description = "The Select SQL Statement used to load the table from the source." + ReportTask.TranslatedParameterDescription });
    task.Parameters.Add(new Parameter() { Name = "source_table", Value = "", DisplayName = "Source table name", Description = "The source table name if the 'Select SQL Statement' is empty."+ ReportTask.TranslatedParameterDescription });
    task.Parameters.Add(new Parameter() { Name = "table_name", Value = "", DisplayName = "Destination table name", Description = "The name of the destination table created in the database. If empty, the source table name is used."+ ReportTask.TranslatedParameterDescription });
    task.Parameters.Add(new Parameter() { Name = "table_prefix", Value = "", DisplayName = "Table name prefix", Description = "Optional prefix added to the destination table name." });
    task.Parameters.Add(new Parameter() { Name = "sql_source_check", Value = "", DisplayName = "Source SQL Check Select", Description = "Optional SQL Select Statement to load a Check table from the source connection. If both source and destination Check tables are identicals, the table is not loaded." });
    task.Parameters.Add(new Parameter() { Name = "sql_dest_check", Value = "", DisplayName = "Destination SQL Check Select", Description = "Optional SQL Select Statement to load a Check table from current source. If both source and destination Check tables are identicals, the table is not loaded." });
    task.Parameters.Add(new Parameter() { Name = "load_burst_size", NumericValue = 0, DisplayName = "Load burst size", Description = "For big tables, number of rows to load. The 'Load sort column' must be also specified. If 0, all the rows are loaded in one shot." });
    task.Parameters.Add(new Parameter() { Name = "load_sort_column", Value = "", DisplayName = "Load sort column", Description = "For big tables, the sort column used to perform the load based on ROW_NUMBER()." });
    task.Parameters.Add(new Parameter() { Name = "insert_burst_size", NumericValue = 2000, DisplayName = "Insert burst size", Description = "The number of rows inserted in one command." });
    task.Parameters.Add(new Parameter() { Name = "column_char_len", NumericValue = 0, DisplayName = "Column char length", Description = "The length of char columns created in the destination table. If 0, the length is calculated from the first load (full or partial)." });

    task.Script = @"@using System.IO
@using System.Data
@{
    ReportTask task = Model;
    var helper = new TaskHelper(task);    

    helper.DatabaseHelper.LoadBurstSize =  task.GetNumericValue(""load_burst_size"");
    helper.DatabaseHelper.LoadSortColumn =  task.GetValue(""load_sort_column"");
    helper.DatabaseHelper.InsertBurstSize =  task.GetNumericValue(""insert_burst_size"");
    helper.DatabaseHelper.ColumnCharLength =  task.GetNumericValue(""column_char_len"");

    var sourceTable = task.GetValueTranslated(""source_table"");
    var sql = task.GetValueTranslated(""sql"");
    if (string.IsNullOrEmpty(sql) && !string.IsNullOrEmpty(sourceTable)) {
        sql = $""select * from {task.Source.GetTableName(sourceTable)}"";
    }

    var destinationTable = task.GetValueTranslated(""table_name"");
    if (string.IsNullOrEmpty(destinationTable)) {
        destinationTable = sourceTable;
    }

    helper.LoadTableFromDataSource(
        task.GetValue(""source_name""), 
        sql, 
        task.GetValue(""table_prefix"") + destinationTable, 
        task.ExecuteForEachConnection, 
        task.GetValue(""sql_source_check""), 
        task.GetValue(""sql_dest_check"")
    );
}
";

    task.BodyScript = ReportTask.BodyScriptTemplate;
}
